package com.angular.aspect;

import java.io.Console;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.transform.impl.AccessFieldTransformer;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.angular.entity.ResponseBody;
import com.angular.service.UserService;

@Aspect
@Order(5)
@Component
public class LoginedValidateAspect {
	@Autowired
	UserService userService;
    //切面点
    @Pointcut("execution(public * com.angular.boot.controller.*.**(..))")
    public void isLogined(){}
    
    @Around("isLogined()")
    public ResponseBody<Object> doBefore(ProceedingJoinPoint pjp) throws Throwable {
     	HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
     	
     	boolean isLogined=false;
     	String url=request.getRequestURL().toString();
     	if(url.endsWith("login")||url.endsWith("permission")) {
     		isLogined=true;
     	}else {
     		isLogined= userService.isLogined(request);
     	}
     	
     	
     	ResponseBody<Object> result=null;
     	
     	if(isLogined) {
     		result= (ResponseBody<Object>) pjp.proceed();
     	}else {
     		result=new ResponseBody<Object>(2,"未登录",null);
     	}
		return result;
    }

    @AfterReturning(returning = "ret", pointcut = "isLogined()")
    public void doAfterReturning(Object ret) throws Throwable {
    	
    	
    }
    
    
    
    
}